package com.geek.factory.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geek.factory.entity.Shelves;
import com.geek.factory.mapper.ShelvesMapper;
import com.geek.factory.service.IShelvesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.Serializable;

@Slf4j
@Service
public class ShelvesServiceImpl extends ServiceImpl<ShelvesMapper, Shelves> implements IShelvesService {

    private static final String CACHE_NAME = "shelves";

    @Override
    @Cacheable(value = CACHE_NAME, key = "#id")
    public Shelves getById(Serializable id) {
        log.info("从数据库查询货柜信息, id: {}", id);
        return super.getById(id);
    }

    @Override
    @CachePut(value = CACHE_NAME, key = "#shelves.shelfId")
    public boolean save(Shelves shelves) {
        boolean result = super.save(shelves);
        log.info("新增货柜信息并写入缓存, id: {}, result: {}", shelves.getShelfId(), result);
        return result;
    }

    @Override
    @CachePut(value = CACHE_NAME, key = "#shelves.shelfId")
    public boolean updateById(Shelves shelves) {
        boolean result = super.updateById(shelves);
        log.info("更新货柜信息并更新缓存, id: {}, result: {}", shelves.getShelfId(), result);
        return result;
    }

    @Override
    @CacheEvict(value = CACHE_NAME, key = "#id")
    public boolean removeById(Serializable id) {
        boolean result = super.removeById(id);
        log.info("删除货柜信息并删除缓存, id: {}, result: {}", id, result);
        return result;
    }
}